<?php
/**
 * This file contains the following classes: {@link clniFileCollector}, 
 * {@link clniFilePatternCollector}.
 * @author Travis Swicegood <development@domain51.com>
 * @author Adrian Mugica <amugica@uversainc.com>
 */

/**
 * The basic file collector
 *
 * @see collect()
 * @see clniFilePatternCollector
 */
class clniFileCollector {

    /**
	* Strips off any kind of slash at the end so as to normalise the path
	*
	* @param string $path    Path to normalise.
	*/
    function _removeTrailingSlash($path) {
		return preg_replace('|[\\/]$|', '', $path);
    }

    /**
	* Scans the directory and adds what it can.
	* @param string $path    Directory to scan.
	*/
    function collect($path) {
	   $path = $this->_removeTrailingSlash($path);
	   if ($handle = opendir($path)) {
		  while (($entry = readdir($handle)) !== false) {
				$this->_handle($path . DIRECTORY_SEPARATOR . $entry);
		  }
		  closedir($handle);
	   }
    }
    
    /**
	* This method determines what should be done with a given file and adds
	* it via {@link GroupTest::addTestFile()} if necessary.
	*
	* This method should be overriden to provide custom matching criteria, 
	* such as pattern matching, recursive matching, etc.  For an example, see
	* {@link SimplePatternCollector::_handle()}.
	*
	* @param string $filename  A filename as generated by {@link collect()}
	* @see collect()
	* @access protected
	*/
    function _handle($file) {
		global $loader;
		if (!is_dir($file)) {
			require_once($file);
		}
    }
}

/**
 * An extension to {@link clniFileCollector} that only adds files matching a
 * given pattern.
 *
 * @see clniFileCollector
 */
class clniFilePatternCollector extends clniFileCollector {
    var $_pattern;
    
    
    /**
	*
	* @param string $pattern   Perl compatible regex to test name against
	*  See {@link http://us4.php.net/manual/en/reference.pcre.pattern.syntax.php PHP's PCRE}
	*  for full documentation of valid pattern.s
	*/
    function clniFilePatternCollector($pattern = '/php$/i') {
	   $this->_pattern = $pattern;
    }
    
    
    /**
	* Attempts to add files that match a given pattern.
	*
	* @see clniFileCollector::_handle()
	* @param string $path    Directory to scan.
	* @access protected
	*/
    function _handle($filename) {
	   if (preg_match($this->_pattern, $filename)) {
		  parent::_handle($filename);
	   }
    }
}

